package com.itheima.service.impl;

import com.itheima.mapper.OrederSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingDTO;
import com.itheima.pojo.OrderSettingUploadDTO;
import com.itheima.service.OrederSettingService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrederSettingService {

    @Autowired
    private OrederSettingMapper orederSettingMapper;
    /**
     * 根据日期修改可预约人数
     * @param orderSettingDTO
     */
    public void updateByDate(OrderSettingDTO orderSettingDTO) {
        orederSettingMapper.update(orderSettingDTO);
    }

    /**
     * 根据日期获取对应数据
     * @param month
     * @return
     */
    public OrderSetting getByMonth(Date month) {
        if (month.toString().equals(null)) {
            LocalDate now = LocalDate.now();
            String nowString = now.toString();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            try {
                Date date = format.parse(nowString);
                month = date;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        OrderSetting orderSetting = orederSettingMapper.getByMonth(month);
        return orderSetting;
    }

    /**
     * 模板下载
     * @param filename
     */
    public void download(String filename) {
        try {
            InputStream in = this.getClass().getClassLoader().getResourceAsStream("template" + filename);
            XSSFWorkbook excel = new XSSFWorkbook(in);
            FileOutputStream on = new FileOutputStream(filename);
            excel.write(on);
            in.close();
            on.close();
            excel.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 读取文件
     * @param upload
     * HttpServletResponse response
     */
    public void upload(OrderSettingUploadDTO upload) {
        try {
            FileInputStream in = new FileInputStream(new File(upload.getExcelFile()));
            XSSFWorkbook excel = new XSSFWorkbook(in);
            XSSFSheet sheet = excel.getSheet("预约设置模板");
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                XSSFRow row = sheet.getRow(i);
                String cellValue1 = row.getCell(0).getStringCellValue();
                Integer cellValue2 = Integer.valueOf(row.getCell(1).getStringCellValue());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = format.parse(cellValue1);
                orederSettingMapper.uploadReservation(date,cellValue2);
            }
            in.close();
            excel.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
